package com.example.emos.api.service.impl;

import cn.hutool.json.JSONArray;
import com.example.emos.api.common.util.JsonUtils;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.controller.form.SelectExamineeForm;
import com.example.emos.api.db.dao.TbExamineeDao;
import com.example.emos.api.db.dao.TbMarkDao;
import com.example.emos.api.service.ExamineeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Supyo
 * @date ：Created in 2021/11/16 15:03
 * @description：
 */
@Service
public class ExamineeServiceImpl implements ExamineeService {

    @Autowired
    TbExamineeDao examineeDao;
    @Autowired
    TbMarkDao markDao;

    @Override
    public PageUtils selectExamineeBypages(HashMap param) {
        ArrayList<HashMap> list = examineeDao.selectExamineeBypages(param);

        //获取personId;
        int personId[] = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            personId[i] = (int) list.get(i).get("person_id");
            System.out.println("personId: " + personId[i]);
        }

        //获取roles和dept
        ArrayList<HashMap> map = new ArrayList<HashMap>();
        for (int i = 0; i < personId.length; i++) {
            map.add(examineeDao.selectRoleandDept(personId[i]));
        }


        //把roles和dept塞进list返回前端
        for (int i = 0; i < map.size(); i++) {
            list.get(i).put("map", map.get(i));
        }

        long count = examineeDao.count(param);
        int start = (Integer) param.get("start");
        int length = (Integer) param.get("length");
        PageUtils pageUtils = new PageUtils(list, count, start, length);
        return pageUtils;
    }

    @Override
    public HashMap selectExaminee(SelectExamineeForm form) {
        HashMap map = examineeDao.selectExaminee(form.getExamId(), form.getPersonId());

        //查询角色
        String roles = markDao.selectRoles(form.getPersonId());
        map.put("roles", roles);


        //根据得分查询评级
        String rate = "";
        double score = (double) map.get("Score");
        //先查考核模板
        int templateId = markDao.selectExamTemplate(form.getExamId());
        //根据考核模板查询指标库
        int rankId = examineeDao.selectRankId(templateId);
        //查询指标库的评级
        String rank = examineeDao.selectRank(rankId);
        //根据得分判断评级
        JSONArray jsonArray = new JSONArray(rank);
        String garade[] = new String[jsonArray.size()];
        String minScore[] = new String[jsonArray.size()];
        String maxScore[] = new String[jsonArray.size()];
        int minscore[] = new int[jsonArray.size()];
        int maxscore[] = new int[jsonArray.size()];
        for (int i = 0; i < garade.length; i++) {
            garade[i] = (String) jsonArray.getJSONObject(i).get("grade");
            minScore[i] = (String) jsonArray.getJSONObject(i).get("minScore");
            maxScore[i] = (String) jsonArray.getJSONObject(i).get("maxScore");
            minscore[i] = Integer.parseInt(minScore[i]);
            maxscore[i] = Integer.parseInt(maxScore[i]);
            if (score >= minscore[i] && score <= maxscore[i]) {
                rate = garade[i];
            }
        }


        //查询评分人的姓名
        int scorer[] = examineeDao.selectScorer(form.getExamId(), form.getPersonId());
        String name[] = new String[scorer.length];
        ArrayList options = new ArrayList();
        for (int i = 0; i < scorer.length; i++) {
            name[i] = markDao.selectPersonName(scorer[i]);
        }
        options = examineeDao.selectOptions(form.getExamId(), form.getPersonId());
        String a[] = new String[options.size()];
        for (int i = 0; i < options.size(); i++) {
            a[i] = StringUtils.strip((String) options.get(i), "[]");
            System.out.println(a[i]);
        }
        System.out.println(options.get(0));
        map.put("rate", rate);
        map.put("name", name);
        map.put("options", a);
        return map;
    }

}
